/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accountManagement.managedbean;

import accountManagement.entity.Employee;
import accountManagement.sessionbean.EmployeeManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Wei Haoyang
 */
@ManagedBean
@ViewScoped
public class SearchManagedBean {

    @EJB
    private EmployeeManager em;
    private String searchName;
    private List<Employee> employees = new ArrayList<Employee>();
    
    @EJB
    EmployeeManager employee;
    
    /** Creates a new instance of SearchManagedBean */
    public SearchManagedBean() {
    }
    
    public List<String> complete(String query) {  
        List<String> results = new ArrayList<String>();  
        
        List<Employee> employeeList= employee.getAllEmployees();
        
        for (Object o:employeeList) {  
            Employee emp = (Employee)o;
            if (emp.getUserName().startsWith(query)){
                results.add(emp.getName());  
            }
        }  
        return results;
    }

    public EmployeeManager getEm() {
        return em;
    }

    public void setEm(EmployeeManager em) {
        this.em = em;
    }

    public String getSearchName() {
        return searchName;
    }

    public void setSearchName(String searchName) {
        this.searchName = searchName;
    }
    
    public void searchByName() throws IOException{
        setEmployees(em.getEmployeeByName(searchName));
        System.out.println(searchName);
        System.out.println(employees.get(0).getName());
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("Employees", employees);
        FacesContext.getCurrentInstance().getExternalContext().redirect("SearchResult.xhtml");
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
    
    
}
